﻿using LuShanTea.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LuShanTea.DTO;
using LuShanTea.Service.Entities;
using System.Data.Entity;

namespace LuShanTea.Service
{
    public class CartRecordService : ICartRecordService
    {
        public long AddNew(long cartId, long goodId, long packId, int count)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Cart> cartBs = new BaseService<Cart>(ctx);
                var cart = cartBs.GetById(cartId);
                if (cart == null)
                {
                    throw new ArgumentException("购物车不存在");
                }
                BaseService<Good> goodBs = new BaseService<Good>(ctx);
                var good = goodBs.GetById(goodId);
                if (good == null)
                {
                    throw new ArgumentException("该商品不存在");
                }
                BaseService<Pack> packBs = new BaseService<Pack>(ctx);
                var pack = packBs.GetById(packId);
                if (pack == null)
                {
                    throw new ArgumentException("该包装不存在");
                }
                BaseService<CartRecord> cartRecordBs = new BaseService<CartRecord>(ctx);
                var cartRecord = cartRecordBs.GetAll().Where(e => e.CartId == cartId).SingleOrDefault(e => e.GoodId == goodId && e.PackId == packId);
                if (cartRecord!=null)
                {
                    cartRecord.Count++;
                    ctx.SaveChanges();
                    return -1;
                }
                CartRecord en = new CartRecord();
                en.CartId = cartId;
                en.Count = count;
                en.GoodId = goodId;
                en.PackId = packId;
                ctx.CartRecords.Add(en);
                ctx.SaveChanges();
                return en.Id;
            }
        }

        public void BuyGood(long cartRecordId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<CartRecord> bs = new BaseService<CartRecord>(ctx);
                var en = bs.GetById(cartRecordId);
                en.IsBuy = true;
                ctx.SaveChanges();
            }
        }

        public CartRecordDTO[] GetBuyCartByCartId(long cartId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Cart> cartBs = new BaseService<Cart>(ctx);
                if (cartBs.GetById(cartId) == null)
                {
                    throw new ArgumentException("找不到购物车");
                }
                BaseService<CartRecord> bs = new BaseService<CartRecord>(ctx);
                return bs.GetAll().AsNoTracking()
                    .Include(e => e.Good)
                    .Include(e => e.Cart.User)
                    .Include(e => e.Pack)
                    .Where(e => e.CartId == cartId && e.IsBuy == true)
                    .ToList().Select(e => ToDTO(e)).ToArray();
            }
        }

        private CartRecordDTO ToDTO(CartRecord en)
        {
            CartRecordDTO dto = new CartRecordDTO();
            dto.CartId = en.CartId;
            dto.Count = en.Count;
            dto.CreateDateTime = en.CreateDateTime;
            dto.GoodId = en.GoodId;
            dto.GoodName = en.Good.Name;
            dto.GoodPre_Price = en.Good.Pre_Price;
            dto.GoodPrice = en.Good.Price;
            dto.Id = en.Id;
            dto.IsBuy = en.IsBuy;
            dto.UserName = en.Cart.User.Name;
            dto.PackId = en.PackId;
            dto.PackName = en.Pack.Name;
            return dto;
        }

        public void MarkDelete(long cartRecordId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<CartRecord> bs = new BaseService<CartRecord>(ctx);
                bs.MakeDelete(cartRecordId);
            }
        }

        public void Update(long cartRecordId, int count, long packId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<CartRecord> bs = new BaseService<CartRecord>(ctx);
                var en = bs.GetById(cartRecordId);
                if (en == null)
                {
                    throw new ArgumentException("购物车记录未找到");
                }
                en.Count = count;
                en.PackId = packId;
                ctx.SaveChanges();
            }
        }

        public CartRecordDTO[] GetNoBuyCartByCartId(long cartId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<Cart> cartBs = new BaseService<Cart>(ctx);
                if (cartBs.GetById(cartId) == null)
                {
                    throw new ArgumentException("找不到购物车");
                }
                BaseService<CartRecord> bs = new BaseService<CartRecord>(ctx);
                return bs.GetAll().AsNoTracking()
                    .Include(e => e.Good)
                    .Include(e => e.Cart.User)
                    .Include(e=>e.Pack)
                    .Where(e => e.CartId == cartId&&e.IsBuy==false)
                    .ToList().Select(e => ToDTO(e)).ToArray();
            }
        }

        public CartRecordDTO[] GetPage(int pageIndex, int dataCount)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<CartRecord> bs = new BaseService<CartRecord>(ctx);
                return bs.GetAll().Where(e => e.IsBuy == false).AsNoTracking()
                    .Include(e => e.Good)
                    .Include(e => e.Cart.User)
                    .Include(e => e.Pack)
                    .OrderBy(e => e.CartId)
                    .Skip((pageIndex - 1) * dataCount)
                    .Take(dataCount)
                    .ToList().Select(e => ToDTO(e)).ToArray();
            }
        }

        public long GetTotalCount()
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<CartRecord> bs = new BaseService<CartRecord>(ctx);
                return bs.GetAll().LongCount();
            }
        }

        public void UpdateCount(long cartRecordId, int count)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<CartRecord> bs = new BaseService<CartRecord>(ctx);
                var en = bs.GetById(cartRecordId);
                if (en == null)
                {
                    throw new ArgumentException("购物车记录未找到");
                }
                en.Count = count;
                ctx.SaveChanges();
            }
        }

        public CartRecordDTO GetById(long cartRecordId)
        {
            using (LuShanTeaContext ctx = new LuShanTeaContext())
            {
                BaseService<CartRecord> bs = new BaseService<CartRecord>(ctx);
                var res = bs.GetAll().AsNoTracking()
                    .Include(e => e.Good)
                    .Include(e => e.Cart.User)
                    .Include(e => e.Pack)
                    .SingleOrDefault(e => e.IsBuy == false && e.Id == cartRecordId);
                return res == null ? null : ToDTO(res);
            }
        }
    }
}
